<template>
  <!-- 登录 -->
  <el-form
    ref="loginForm"
    :model="loginUser"
    label-width="100px"
    :rules="rules"
    class="loginForm sign-in-form"
  >
    <el-form-item label="邮箱" prop="email">
      <el-input
        v-model="loginUser.email"
        placeholder="Enter Email ...."
      ></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        v-model="loginUser.password"
        placeholder="Enter Password ..."
      ></el-input>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        class="submit-btn"
        @click="handleLogin('loginForm')"
        >提交</el-button
      >
    </el-form-item>
    <!-- 找回密码 -->
    <div class="tiparea">
      <p>忘记密码？ <a @click="handleForget">立即找回</a></p>
    </div>
  </el-form>
</template>

<script lang="ts">
import { defineComponent, getCurrentInstance } from "vue";
import {useRouter} from 'vue-router'

export default defineComponent({
  props: {
    loginUser: {
      type: Object,
      required: true,
    },
    rules: {
      type: Object,
      required: true,
    },
  },
  setup(props) {
    const { ctx } = getCurrentInstance() as any;
    const router = useRouter()

    // 登录方法
    const handleLogin: (formName: string) => void = (formName: string) => {
      ctx.$refs[formName].validate((valid: boolean) => {
        if (valid) {
          ctx.$refs[formName].$axios.post('/api/v1/auth/login',props.loginUser).then((res:any) => {
            // @ts-ignore
            // ctx.$message.success('注册成功')
            const {token} = res
            localStorage.setItem('msToken', token)
            // 路由跳转
            router.push('/')
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    };

    // 找回密码
    const handleForget:() => void = () => {
      router.push('/forgotpassword')
    }

    return { handleLogin , handleForget};
  },
});
</script>

<style scoped>
.loginForm {
  margin-top: 20px;
  background-color: #fff;
  padding: 20px 40px 20px 20px;
  border-radius: 5px;
  box-shadow: 0px 5px 10px #cccc;
}
.submit-btn {
  width: 100%;
}
.tiparea {
  text-align: right;
  font-size: 12px;
  color: #333;
}
.tiparea p a {
  color: #409eff;
}
</style>